//
//  SSEditViewController.m
//  list
//
//  Created by sujeking on 2017/8/9.
//  Copyright © 2017年 szw. All rights reserved.
//

#import "SSEditViewController.h"
#import "SSMenuViewController.h"

#define kWidth [[UIScreen mainScreen] bounds].size.width


@interface SSEditViewController () <SSMenuViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITextView *mTextView;
@property (weak, nonatomic) IBOutlet UIView *mToolView;
@property (weak, nonatomic) IBOutlet UIButton *mBtnKeyBoard;
@property (weak, nonatomic) IBOutlet UIButton *mBtnFont;
@property (weak, nonatomic) IBOutlet UIButton *mBtnDone;
@property (strong, nonatomic) SSMenuViewController *menuvc;
@property (strong, nonatomic) NSArray *strings;
@property (nonatomic) NSRange currentSelectRange;
@property (strong, nonatomic) NSMutableAttributedString *content;

@end

@implementation SSEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self.menuvc addObserver:self forKeyPath:@"show" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"show"]) {
        self.menuvc.show ? [self showMenuVCAction] : [self hiddenMenuVCAction];
    }
}

// MARK: - Init
- (void)setupUI {
    [self addChildViewController:self.menuvc];
    self.menuvc.show = NO;
    
    CGFloat y = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    self.menuvc.view.frame = (CGRect){0,y,kWidth,[self menuVCHeight]};
    [self.view addSubview:self.menuvc.view];
    
    NSAttributedString *txt = [[NSAttributedString alloc] initWithString:@"输入你的内容" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    self.content = [[NSMutableAttributedString alloc] initWithAttributedString:txt];
    self.mTextView.attributedText = txt;
    [self.mTextView becomeFirstResponder];
}

// MARK: - Action

- (IBAction)keyboardBtnClickAction:(id)sender {
    self.menuvc.show = NO;
    [self.mTextView reloadInputViews];
}

- (IBAction)fontBtnClickAction:(id)sender {
    self.currentSelectRange = self.mTextView.selectedRange;
    self.strings = [self.mTextView.attributedText.string componentsSeparatedByString:@"\n"];
    self.menuvc.show = YES;
    self.mTextView.inputView = [[UIView alloc] initWithFrame:CGRectZero];
    self.mTextView.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.mTextView reloadInputViews];
    self.mTextView.inputView = nil;
    self.mTextView.inputAccessoryView =  nil;
}

- (IBAction)doneBtnClickAction:(id)sender {
    //todo
}

- (void)showMenuVCAction {
    [self.menuvc didMoveToParentViewController:self];
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat y = CGRectGetMaxY(self.mToolView.frame) + 1;
        self.menuvc.view.frame = (CGRect){0,y,kWidth,[self menuVCHeight]};
    }];
}

- (void)hiddenMenuVCAction {
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat y = CGRectGetHeight([[UIScreen mainScreen] bounds]);
        self.menuvc.view.frame = (CGRect){0,y,kWidth,[self menuVCHeight]};
    }];
}

// MARK: - SSMenuViewControllerDelegate

- (void)ss_menuViewcontrollerForAlignment:(NSTextAlignment)alignment {
    self.currentSelectRange = self.mTextView.selectedRange;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = alignment;
    self.content = [self.mTextView.attributedText mutableCopy];
    [self.content addAttribute:NSParagraphStyleAttributeName value:style range:[self editRange]];
    [self updateTextView];
}

- (void)ss_menuViewcontrollerForUIFont:(UIFont *)font {
    self.currentSelectRange = self.mTextView.selectedRange;
    self.content = [self.mTextView.attributedText mutableCopy];
    [self.content addAttributes:@{NSFontAttributeName:font} range:[self editRange]];
    [self updateTextView];
}

- (void)ss_menuViewcontrollerForTextColor:(UIColor *)color {
    self.currentSelectRange = self.mTextView.selectedRange;
    self.content = [self.mTextView.attributedText mutableCopy];
    [self.content addAttributes:@{NSForegroundColorAttributeName:color} range:[self editRange]];
    [self updateTextView];
}

- (void)updateTextView {
    self.mTextView.attributedText = self.content;
    self.mTextView.selectedRange = self.currentSelectRange;
}

// MARK: - GET

- (SSMenuViewController *)menuvc {
    if (!_menuvc) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        _menuvc = (SSMenuViewController *)[sb instantiateViewControllerWithIdentifier:@"SSMenuViewController"];
        _menuvc.delegate = self;
    }
    return _menuvc;
}

- (CGFloat)menuVCHeight {
    CGFloat SHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    CGFloat maxYTool = CGRectGetMaxY(self.mToolView.frame);
    return SHeight - maxYTool;
}

- (NSRange)editRange {
    NSInteger row = [self currentRow];
    CGFloat length = [self.strings[row] length];
    return NSMakeRange([self frontLength], length);
}

- (CGFloat)frontLength {
    CGFloat length = [self.strings[[self currentRow]] length];
    NSInteger location = self.mTextView.selectedRange.location;
    return location - length;
}

- (NSInteger)currentRow {
    NSInteger location = self.mTextView.selectedRange.location;
    NSString *text = [self.mTextView.attributedText.string substringToIndex:location];
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    return arr.count - 1;
}

@end
